package com.erp.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.erp.dao.PublicDao;
import com.erp.model.OrderPurchase;
import com.erp.model.OrderPurchaseLine;
import com.erp.service.OrderPurchaseService;
import com.erp.util.Constants;
import com.erp.util.PageUtil;
@Service("orderPurchaseService")
@SuppressWarnings("unchecked")
public class OrderPurchaseServiceImpl implements OrderPurchaseService
{
	@SuppressWarnings("rawtypes")
	@Autowired
	private PublicDao dao;
	
	
	@Override
	public List<OrderPurchase> findPurchaseOrderList(Map<String, Object> map,
			PageUtil pageUtil)
	{
		String  hql="from OrderPurchase t where t.status='A' ";
		hql+=Constants.getSearchConditionsHQL("t", map);
		hql+=Constants.getGradeSearchConditionsHQL("t", pageUtil);
		return dao.find(hql, map, pageUtil.getPage(), pageUtil.getRows());
	}

	@Override
	public Long getCount(Map<String, Object> map, PageUtil pageUtil)
	{
		String hql="select count(*) from OrderPurchase t where t.status='A' ";
		hql+=Constants.getSearchConditionsHQL("t", map);
		hql+=Constants.getGradeSearchConditionsHQL("t", pageUtil);
		return dao.count(hql, map);
	}

	@Override
	public boolean delOrderPurchase(Integer orderPurchaseId)
	{
		Integer userId=Constants.getCurrendUser().getUserId();
		OrderPurchase c = (OrderPurchase)dao.get(OrderPurchase.class, orderPurchaseId);
		c.setLastmod(new Date());
		c.setModifyer(userId);
		c.setStatus(Constants.PERSISTENCE_DELETE_STATUS);
		dao.deleteToUpdate(c);
		String hql="from OrderPurchaseLine t where t.status='A' and t.orderPurchaseId="+orderPurchaseId;
		List<OrderPurchaseLine> list = dao.find(hql);
		for (OrderPurchaseLine cus : list)
		{
			cus.setLastmod(new Date());
			cus.setModifyer(userId);
			cus.setStatus(Constants.PERSISTENCE_DELETE_STATUS);
			dao.deleteToUpdate(cus);
		}
		return true;
	}

}
